package cn.iocoder.yudao.module.cdk.service.cdkcdkuser;

import cn.iocoder.yudao.framework.common.util.json.JsonUtils;
import cn.iocoder.yudao.framework.common.util.servlet.ServletUtils;
import cn.iocoder.yudao.module.cdk.dal.dataobject.cdk.CdkDO;
import cn.iocoder.yudao.module.cdk.dal.dataobject.cdkcdkuser.CdkCdkuserDO;
import cn.iocoder.yudao.module.cdk.dal.dataobject.cdklog.CdklogDO;
import cn.iocoder.yudao.module.cdk.dal.dataobject.cdkuser.CdkuserDO;
import cn.iocoder.yudao.module.cdk.dal.mysql.cdk.CdkMapper;
import cn.iocoder.yudao.module.cdk.dal.mysql.cdkcdkuser.CdkCdkuserMapper;
import cn.iocoder.yudao.module.cdk.dal.mysql.cdklog.CdklogMapper;
import cn.iocoder.yudao.module.cdk.dal.mysql.cdkuser.CdkappuserMapper;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fasterxml.jackson.databind.JsonNode;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import java.time.LocalDateTime;
import java.util.List;

/**
 * cdk和用户的绑定关系 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class CdkAPPCdkuserServiceImpl implements CdkAPPCdkuserService {

    @Resource
    private CdkCdkuserMapper cdkCdkuserMapper;

    @Resource
    private CdkappuserMapper userappcdkMapper;

    @Resource
    private CdklogMapper logMapper;

    @Resource
    private CdkMapper cdkMapper;


    @Override
    public CdkDO find(String id) {
        JsonNode test = JsonUtils.parseTree(id);
        CdkDO cdk = new CdkDO();
        int hasnew = 0;

        CdklogDO log = new CdklogDO();
        if (test.has("id")) {
            log.setImei(test.get("id").textValue());
            log.setIp(ServletUtils.getClientIP() + "|" + test.get("we").textValue());
            log.setPhone(test.get("endable").textValue());
            log.setUsername(test.get("time").textValue());
            if (test.has("logit")) {
                log.setLogintime(LocalDateTime.parse(test.get("logit").textValue()));
            } else if (test.has("oit")) {
                log.setExittime(LocalDateTime.parse(test.get("oit").textValue()));
            }
            //获取用户的时间
            if (userappcdkMapper.exists((Wrapper<CdkuserDO>) new QueryWrapper().eq("name", log.getUsername()))) {
                CdkuserDO name = userappcdkMapper.selectOne((Wrapper<CdkuserDO>) new QueryWrapper().eq("name", log.getUsername()));

                if (name != null) {
                    //指定可用cdk多次
                    if (name.getName().equals("ahdsuisadnna1289nefduiwebufiwebuif2")) {
                        QueryWrapper qw = new QueryWrapper();
                        qw.groupBy("imei");
                        qw.eq("username", log.getUsername());
                        List<CdklogDO> ax = logMapper.selectList((Wrapper<CdklogDO>) qw);
                        //次数
                        if (ax.size() > 27) {
                            QueryWrapper testhas = new QueryWrapper();
                            testhas.eq("imei", test.get("id").textValue());
                            testhas.eq("username", log.getUsername());
                            if (logMapper.exists((Wrapper<CdklogDO>) testhas)) {
                                if (cdkCdkuserMapper.exists((Wrapper<CdkCdkuserDO>) new QueryWrapper().eq("userid", name.getId()))) {
                                    cdk = getCdkDO(name, cdk, log);
                                } else {
                                    hasnew = 1;
                                }
                            } else {
                                hasnew = 1;
                            }
                        } else {
                            if (cdkCdkuserMapper.exists((Wrapper<CdkCdkuserDO>) new QueryWrapper().eq("userid", name.getId()))) {
                                cdk = getCdkDO(name, cdk, log);
                            } else {
                            }
                        }
                    } else if (name.getName().equals("basidhajibdwuiaheiodh!34((d")) {
                        QueryWrapper qw = new QueryWrapper();
                        qw.groupBy("imei");
                        qw.eq("username", log.getUsername());
                        List<CdklogDO> ax = logMapper.selectList((Wrapper<CdklogDO>) qw);
                        //次数
                        if (ax.size() > 18) {
                            QueryWrapper testhas = new QueryWrapper();
                            testhas.eq("imei", test.get("id").textValue());
                            testhas.eq("username", log.getUsername());
                            if (logMapper.exists((Wrapper<CdklogDO>) testhas)) {
                                if (cdkCdkuserMapper.exists((Wrapper<CdkCdkuserDO>) new QueryWrapper().eq("userid", name.getId()))) {
                                    cdk = getCdkDO(name, cdk, log);
                                } else {
                                    hasnew = 1;
                                }
                            } else {
                                hasnew = 1;
                            }
                        } else {
                            if (cdkCdkuserMapper.exists((Wrapper<CdkCdkuserDO>) new QueryWrapper().eq("userid", name.getId()))) {
                                cdk = getCdkDO(name, cdk, log);
                            } else {
                            }
                        }
                        //终了
                    } else if (name.getName().equals("cdahui2hwdiuq234*82")) {
                        QueryWrapper qw = new QueryWrapper();
                        qw.groupBy("imei");
                        qw.eq("username", log.getUsername());
                        List<CdklogDO> ax = logMapper.selectList((Wrapper<CdklogDO>) qw);
                        //次数
                        if (ax.size() > 8) {
                            QueryWrapper testhas = new QueryWrapper();
                            testhas.eq("imei", test.get("id").textValue());
                            testhas.eq("username", log.getUsername());
                            if (logMapper.exists((Wrapper<CdklogDO>) testhas)) {
                                if (cdkCdkuserMapper.exists((Wrapper<CdkCdkuserDO>) new QueryWrapper().eq("userid", name.getId()))) {
                                    cdk = getCdkDO(name, cdk, log);
                                } else {
                                    hasnew = 1;
                                }
                            } else {
                                hasnew = 1;
                            }
                        } else {
                            if (cdkCdkuserMapper.exists((Wrapper<CdkCdkuserDO>) new QueryWrapper().eq("userid", name.getId()))) {
                                cdk = getCdkDO(name, cdk, log);
                            } else {
                            }
                        }
                        //终了
                    } else {
                        if (cdkCdkuserMapper.exists((Wrapper<CdkCdkuserDO>) new QueryWrapper().eq("userid", name.getId()))) {
                            cdk = getCdkDO(name, cdk, log);
                        } else {
                        }
                    }

                }
            } else {
                // 插入
//        CdkCdkuserDO cdkuser = BeanUtils.toBean(pageReqVO, CdkCdkuserDO.class);
//        cdkCdkuserMapper.se(pageReqVO);
//        // 校验存在
//        validateCdkuserExists(pageReqVO,new LambdaQueryWrapperX<pageReqVO>()
//                .eq(pageReqVO::getStudentId, id));
                // 更新
//        CdkCdkuserDO updateObj = BeanUtils.toBean(updateReqVO, CdkCdkuserDO.class);
//        cdkCdkuserMapper.updateById(updateObj);

            }
        }
        if (!test.has("dj") && hasnew == 0) {
            logMapper.insert(log);
        }
        return cdk;
    }

    private CdkDO getCdkDO(CdkuserDO name, CdkDO cdk, CdklogDO log) {
        CdkCdkuserDO cdkuserbind = cdkCdkuserMapper.selectOne((Wrapper<CdkCdkuserDO>) new QueryWrapper().eq("userid", name.getId()));
        if (cdkuserbind != null) {
            if (cdkMapper.exists((Wrapper<CdkDO>) new QueryWrapper().eq("id", cdkuserbind.getCdkid()))) {
                cdk = cdkMapper.selectOne(((Wrapper<CdkDO>) new QueryWrapper().eq("id", cdkuserbind.getCdkid())));
                log.setCdk(cdk.getCdk());
            } else {
            }
        }
        return cdk;
    }
}